package com.lecture.java8.excel.template;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;

import java.util.*;

public class TemplateDemo2 {
    public static void main(String[] args) {
        String templatePath = "D:\\study\\code\\spring-lecture\\src\\main\\java\\com\\example\\java8\\excel\\template\\template2.xlsx";

        String fileName = "D:\\workspace\\templateTest3.xlsx";

        ExcelWriter excelWriter = null;
        try {
            excelWriter = EasyExcel.write(fileName).withTemplate(templatePath).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
            // 如果有多个list 模板上必须有{前缀.} 这里的前缀就是 data1，然后多个list必须用 FillWrapper包裹
            excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet);

            Map<String, Object> map = new HashMap<>();
            map.put("columnName", "张三");
            map.put("data1", Arrays.asList("a","b"));
            excelWriter.fill(map,fillConfig,writeSheet);
            excelWriter.fill(map, writeSheet);
        } finally {
            // 千万别忘记关闭流
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }

    public static List<TemplateData> data() {
        List<Double> list = new ArrayList<>();

        list.add(1.4);
        list.add(1.5);
        list.add(1.6);
        list.add(1.7);

        List<TemplateData> dataList = new ArrayList<>();
        dataList.add(new TemplateData("A"));
        dataList.add(new TemplateData("B"));
        dataList.add(new TemplateData("C"));
        dataList.add(new TemplateData("D"));
        //return list.stream().map(Object::toString).collect(Collectors.toList());
        return dataList;
    }

    public static void main2(String[] args) {
        /*JedisPool pool = new JedisPool("localhost", 6379);

        try (Jedis jedis = pool.getResource()) {
            Pipeline pipeline = jedis.pipelined();

            // 添加多个命令到 pipeline
            pipeline.set("key1", "value1");
            pipeline.set("key2", "value2");
            pipeline.get("key1");
            pipeline.get("key2");

            // 执行所有命令
            List<Object> results = pipeline.syncAndReturnAll();

            // 处理返回结果
            for (Object result : results) {
                System.out.println(result);
            }
        }*/
    }
}
